<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="选项组名称:" prop="cid">
        <el-select size="mini" v-model="queryParams.cid" @change="getList">
          <el-option
            @click.native="changeCategorize(item)"
            v-for="item in categoryOptions"
            :key="item.id"
            :label="item.name + '（' + item.type + '）'"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="选项名称:" prop="name">
        <el-input
          size="mini"
          v-model="queryParams.name"
          placeholder="请输入选项名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:policyOption:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="选项名称" align="center" prop="name"/>
      <el-table-column label="排序号" align="center" prop="sort"/>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:policyOption:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:policyOption:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="类型:">
          <el-input v-model="form.ctype" :disabled="true"/>
        </el-form-item>
        <el-form-item label="选项组名称:">
          <el-input v-model="form.cname" :disabled="true"/>
        </el-form-item>
        <el-form-item label="选项名称:" prop="name">
          <el-input v-model="form.name" placeholder="请输入选项名称"/>
        </el-form-item>
        <el-form-item label="显示排序:" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {optionSelect, getCategoryInfo, pageList, add, edit, remove, getInfo} from "@/api/triones/policyOption";

export default {
  name: "Data",
  data() {
    return {
      showSearch: true,
      // 遮罩层
      loading: true,
      // 选项组名称
      categoryOptions: [],
      // 列表
      dataList: [],
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cid: undefined,
        cname: undefined,
        ctype: undefined
      },
      defaultCid: undefined,
      defaultCname: undefined,
      defaultCtype: undefined,
      title: "",
      open: false,
      form: {},
      rules: {
        name: [
          {required: true, message: "选项名称不能为空", trigger: "blur"}
        ]
      },
    }
  },
  created() {
    const cid = this.$route.params && this.$route.params.id;
    this.getCategoryInfo(cid);
    this.getCategoryList();
  },
  methods: {
    changeCategorize(item) {
      this.queryParams.cid = item.id;
      this.queryParams.cname = item.name;
      this.queryParams.ctype = item.type;
    },
    getCategoryInfo(cid) {
      getCategoryInfo(cid).then(res => {
        this.queryParams.cid = res.data.id;
        this.queryParams.cname = res.data.name;
        this.queryParams.ctype = res.data.type;
        this.defaultCid = res.data.id;
        this.defaultCname = res.data.name;
        this.defaultCtype = res.data.type;
        this.getList();
      })
    },
    getCategoryList() {
      this.loading = true;
      optionSelect().then(response => {
        this.categoryOptions = response.data;
      });
    },
    getList() {
      this.loading = true;
      pageList(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 返回按钮操作 */
    handleClose() {
      const obj = { path: "/triones/policy/policyOption" };
      this.$tab.closeOpenPage(obj);
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.cid = this.defaultCid;
      this.queryParams.cname = this.defaultCname;
      this.queryParams.ctype = this.defaultCtype;
      this.handleQuery();
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加选项数据";
      this.form.cid = this.queryParams.cid;
      this.form.cname = this.queryParams.cname;
      this.form.ctype = this.queryParams.ctype;
    },
    reset() {
      this.form = {
        id: undefined,
        cid: undefined,
        cname: undefined,
        ctype: undefined,
        name: undefined,
        sort: 0
      };
      this.resetForm("form");
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.open = true;
          if (this.form.id != null) {
            edit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id
      getInfo(id).then(response => {
        this.form = response.data;
        this.title = "修改选项数据";
        this.open = true;
      })
    },
    handleDelete(row) {
      this.$modal.confirm('是否确认删除选项名称为"' + row.name + '"的数据项？').then(function () {
        return remove(row.id);
      }).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      }).catch(() => {});
    },
  }
}
</script>
<style scoped lang="scss">

</style>
